package club.hicode.daydayup.netty.websocket.client;

import cn.hutool.core.util.CharsetUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;


/**
 * XXXX
 *
 * @author LiuChunfu
 * @date 2018/6/18
 */
public class ClientInitlizer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast("encoder", new StringEncoder());
        pipeline.addLast("decoder", new StringDecoder());
        pipeline.addLast("delimeter",
                new DelimiterBasedFrameDecoder
                        (1024 * 8, Unpooled.copiedBuffer("$".getBytes(CharsetUtil.CHARSET_UTF_8))));
        pipeline.addLast("hanlder", new ClientHandler());
    }
}
